package icu.cocoon.work.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import icu.cocoon.dao.base.BaseController;
import icu.cocoon.dao.base.Resp;
import icu.cocoon.work.entity.WeekContentItem;
import icu.cocoon.work.entity.WeekReport;
import icu.cocoon.work.pojo.param.WeekItemGenerateParam;
import icu.cocoon.work.service.WeekContentItemService;
import icu.cocoon.work.service.WeekReportService;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Cocoon
 * @since 2020-06-12
 */
@RestController
@RequestMapping("/work/weekContentItem")
public class WeekContentItemController extends BaseController<WeekContentItem, WeekContentItemService> {

  @Resource
  private WeekReportService weekReportService;

  @PostMapping("generate/{weekReportId:\\d+}")
  public Resp<?> generate(@PathVariable Long weekReportId, @RequestBody WeekItemGenerateParam param) {
    WeekReport weekReport = weekReportService.getById(weekReportId);
    LocalDate startDate = weekReport.getStartDate();
    List<WeekContentItem> itemList = new ArrayList<>();
    for (String s : param.getFunctionItemList()) {
      WeekContentItem weekContentItem = new WeekContentItem();
      weekContentItem.setWeekReportId(weekReportId);
      weekContentItem.setContent(param.getProjectName() + "-" + s);
      weekContentItem.setPlanCompleteDate(startDate);
      weekContentItem.setRealCompleteDate(startDate);
      startDate = startDate.plusDays(1);
      itemList.add(weekContentItem);
    }
    getService().create(itemList);
    return Resp.success();
  }


  @Override
  protected IPage<WeekContentItem> getPage(Map<String, String> params) {
    long page = 0;
    long size = 10;
    if (StringUtils.isNotBlank(params.get("page"))) {
      page = Integer.parseInt(params.get("page"));
      params.remove("page");
    }
    if (StringUtils.isNotBlank(params.get("size"))) {
      size = Integer.parseInt(params.get("size"));
      params.remove("size");
    }
    return new Page<WeekContentItem>(page, size).addOrder(OrderItem.asc("id"));

  }

}

